package com.hot12345.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hot12345.common.basedata.Constant;
import com.hot12345.common.util.DateUtil;
import com.hot12345.dao.ClickDaoImpl;
import com.hot12345.dao.InfoDaoImpl;
import com.hot12345.entity.Click;
import com.hot12345.entity.Info;

public class ClickServiceImpl {
	private final static Log log = LogFactory.getLog(ClickServiceImpl.class);
	
	private final static ClickServiceImpl instance = new ClickServiceImpl();
	private ClickServiceImpl(){}
	
	public static ClickServiceImpl getInstance(){
		return instance;
	}

	public List<Click> getClickList(int dateType, Integer itemID, int startNo, int pageSize) throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("state", 1);
		params.put("itemID", itemID);
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(dateType==Constant.dateType_day){
			//当天
			params.put("mainFlag", 1);
			params.put("date_start", DateUtil.getToDayStartTime());
			params.put("date_end", DateUtil.getToDayLastTime());
			log.debug(df.format(DateUtil.getToDayStartTime()));
			log.debug(df.format(DateUtil.getToDayLastTime()));
		}else if(dateType==Constant.dateType_24Hour){
			//24小时
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.HOUR_OF_DAY, -24);
			params.put("date_start",cal.getTime());
			log.debug(df.format(DateUtil.getToDayStartTime()));
			log.debug(df.format(DateUtil.getToDayLastTime()));
		} else if(dateType==Constant.dateType_week){
			//一周
			Calendar cal = Calendar.getInstance();
			cal.setTime(DateUtil.getToDayLastTime());
			cal.add(Calendar.WEEK_OF_MONTH, -1);
			params.put("date_start", cal.getTime());
			log.debug(df.format(DateUtil.getToDayLastTime()));
			log.debug(df.format(cal.getTime()));
		} else if(dateType==Constant.dateType_month){
			//一月
			Calendar cal = Calendar.getInstance();
			cal.setTime(DateUtil.getToDayLastTime());
			cal.add(Calendar.MONTH, -1);
			params.put("date_start", cal.getTime());
			log.debug(df.format(DateUtil.getToDayStartTime()));
			log.debug(df.format(cal.getTime()));
		} else {
			//最近半年
			Calendar cal = Calendar.getInstance();
			cal.setTime(DateUtil.getToDayLastTime());
			cal.add(Calendar.MONTH, -6);
			params.put("date_start", cal.getTime());
			log.debug(df.format(DateUtil.getToDayStartTime()));
			log.debug(df.format(cal.getTime()));
		}
		
		ClickDaoImpl clickDao = ClickDaoImpl.getInstance();
		List<Click> list = clickDao.findByTop(params, startNo, pageSize);
		
		return list;
	}
	
	/**
	 * 模拟一个用户点击
	 *	
	 * @param id 信息ID
	 * @param ip
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-5-11 上午12:54:55
	 * @version V1.0
	 */
	public static void addClickNum(long id,String ip) throws Exception{
		InfoDaoImpl infoDao = InfoDaoImpl.getInstance();
		Info info = infoDao.findByPrimaryKey(id);
		if(info==null){
			log.error("增加点击数错误，info==null,id="+id);
			return;
		}
		
		if(info.getParentID()==null){
			log.error("增加点击数错误，info.getParentID()==null,id="+id);
			return;
		}
		
//		String ip = IPUtil.getUserIP(request);
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("infoID", id);
		params.put("parentID", id);// infoID及parentID相同的为该组信息的总访问数
		ClickDaoImpl clickDao = ClickDaoImpl.getInstance();
		Click click = clickDao.findUniqueByParams(params);
		
		if(click==null){
			click = new Click();
			click.setDate(new Date());
			click.setInfoID(id);
			click.setItemID(info.getItemID());
			click.setParentID(id);
			click.setNum(0);
			click.setIp(ip);
			click.setState(1);
			clickDao.insertL(click);
		}
		
		//固定插入一条记录，以后统计每种类型访问总数的时候需要到。
		click = new Click();
		click.setDate(new Date());
		click.setInfoID(id);
		click.setItemID(info.getItemID());
		click.setParentID(info.getParentID());
		click.setNum(1);
		click.setSiteID(info.getSiteID());
		click.setIp(ip);
		click.setState(1);
		clickDao.insertL(click);

		clickDao.addNum(id);//主记录更新访问总数
	}
}
